package segmenter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;


public class SegmenterFactory {

	public void segment(int type, String dbName, String paperLinkPath,
			String segResultFilePath)  throws Exception{
		if (type != 0 && type != 1) {
			return;
		} else if (dbName == null || paperLinkPath == null
				|| segResultFilePath == null)
			return;
		Mongo mg = new Mongo("10.108.102.166",27017);//连接mongo
		if(type==0){//中文分词
			ChineseSegment chineseSegment = ChineseSegment.getInstance();
			DB db= mg.getDB(dbName);
			DBCollection coll = db.getCollection("chinesePaperInfo"); 	
			BufferedWriter bWriter1=new BufferedWriter(new FileWriter(new File(paperLinkPath)));
			BufferedWriter bWriter2=new BufferedWriter(new FileWriter(new File(segResultFilePath)));
			bWriter1.write("");
			bWriter2.write("");
			int total = coll.find().count();
			int num=0;
			for(int skipTemp = 0;skipTemp<=total/10000;skipTemp++){
	    		DBCursor cur = coll.find().skip(skipTemp*10000).limit(10000).addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);	    		
	        	while(cur.hasNext())
	        	{
	        		num++;
	        		BasicDBObject DBObj = (BasicDBObject)cur.next();
	        		String paperid = DBObj.getString("paperLink");

	        		if(paperid==null)
	        			continue;
	        		String contentSeg = null;	
	        		StringBuilder sb=new StringBuilder();
	        		
	        		if(DBObj.getString("chineseAbstract") != null){
	    				String Abs = DBObj.getString("chineseAbstract");
	    				System.out.println("before:" + Abs);
	    				sb.append(Abs+" ");
	    			}
	    			if(DBObj.getString("chineseTitle") != null){
	    				String Title = DBObj.getString("chineseTitle");
	    				sb.append(Title+" ");
	    			}
	    			
	    				
	        		if(DBObj.get("chindeseKeyword") != null){
	        			ArrayList<BasicDBObject> versi = (ArrayList<BasicDBObject>)DBObj.get("chineseKeyword");
	    	    		int curAuthorNum=0;
	    				for(BasicDBObject embedded:versi)
	    				{
	    					curAuthorNum++;
	    				    String key = embedded.getString("chineseKeyword");
	    				    sb.append(key+" ");
	    				}
	        		}
	        		contentSeg = sb.toString();
	        		String result = chineseSegment.segmentSen(contentSeg);
	        		System.out.println("after:" + result);
	        		bWriter1.write(paperid + "\n");
	        		bWriter1.flush();
	        		bWriter2.write(result + "\n");
	        		bWriter2.flush();
	        		contentSeg = null;
	        		result = null;
	        		sb = null;
	        	}
	        	
	        	cur.close();
			}
			bWriter1.close();
        	bWriter2.close();
			mg.close();
		}
		else{
			
			DB db= mg.getDB(dbName);
			DBCollection coll = db.getCollection("englishPaperInfo"); 	
			BufferedWriter bWriter1=new BufferedWriter(new FileWriter(new File(paperLinkPath)));
			BufferedWriter bWriter2=new BufferedWriter(new FileWriter(new File(segResultFilePath)));
			bWriter1.write("");
			bWriter2.write("");
			int total = coll.find().count();
			int num=0;
			EnglishSegment englishSegment = new EnglishSegment(true);
			englishSegment.readStopWords("models/swlist.txt");
			for(int skipTemp = 0;skipTemp<=total/10000;skipTemp++){
	    		DBCursor cur = coll.find().skip(skipTemp*10000).limit(10000).addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);	    		
	        	while(cur.hasNext())
	        	{
	        		num++;
	        		BasicDBObject DBObj = (BasicDBObject)cur.next();
	        		String paperid = DBObj.getString("paperLink");

	        		if(paperid==null)
	        			continue;
	        		String contentSeg = null;	
	        		StringBuilder sb=new StringBuilder();
	        		
	        		if(DBObj.getString("abstract") != null){
	    				String Abs = DBObj.getString("abstract");
	    				System.out.println("before:" + Abs);
	    				sb.append(Abs+" ");
	    			}
	    			if(DBObj.getString("title") != null){
	    				String Title = DBObj.getString("title");
	    				sb.append(Title+" ");
	    			}
	    			
	    				
	        		if(DBObj.get("keyword") != null){
	        			ArrayList<BasicDBObject> versi = (ArrayList<BasicDBObject>)DBObj.get("keyword");
	    	    		int curAuthorNum=0;
	    				for(BasicDBObject embedded:versi)
	    				{
	    					curAuthorNum++;
	    				    String key = embedded.getString("keyword");
	    				    sb.append(key+" ");
	    				}
	        		}
	        		contentSeg = sb.toString();
	        		String result = englishSegment.splitWord(contentSeg);
	        		System.out.println("after:" + result);
	        		bWriter1.write(paperid + "\n");
	        		bWriter1.flush();
	        		bWriter2.write(result + "\n");
	        		bWriter2.flush();
	        		contentSeg = null;
	        		result = null;
	        		sb = null;
	        	}
	        	
	        	cur.close();
			}
			bWriter1.close();
        	bWriter2.close();
			mg.close();
			
		}
	}
	public static void main(String []args){
		SegmenterFactory sFactory = new SegmenterFactory();
		try {
			sFactory.segment(1, "paperRecommender", "/home/hadoop/mwn/test2Ling.txt", "/home/hadoop/mwn/test2result.txt");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
